<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            position: absolute;
            top: 100px;
        }

        div:first-of-type {
            background: red;
            left: 200px;
        }

        div:last-of-type {
            background: blue;
            right: 200px;
        }

        a {
            display: block;
            font-size: 50px;
            text-decoration: none;
            text-align: center;
        }
    </style>
</head>

<body>
    <div></div>
    <a href="http://baidu.com">等下我会自动跳转</a>
    <div></div>

    <script>
        var divs = document.querySelectorAll('div');
        var a = document.querySelector('a');
        setTimeout(function () {
            // 创建一个 MouseEvent 模拟鼠标事件
            var event = new MouseEvent('click', {
                bubbles: true,
                cancelable: true,
                isTrusted: true,
                view: window
            })
            // dispatchEvent 事件的触发器
            a.dispatchEvent(event);
            divs[0].dispatchEvent(event);
            divs[1].dispatchEvent(event);

        }, 1000);

        // addEventListener 监听click事件， 并作出相应的动作
        divs[0].addEventListener('click', () => {
            divs[0].style.background = 'yellow';
        })
        divs[1].addEventListener('click', () => {
            divs[1].style.background = 'pink';
        })
        a.addEventListener('click', () => {
            console.log('自动点击跳转页面')
        })
    </script>
</body>

</html>